import mysql.connector
def hello():
    print (1)

class Sql:
    def __init__(self, user, password,database,host,port):
        self.database = database
        self.conn = mysql.connector.connect(user=user, password=password, database=database,host=host,port=port)
        self.cursor = self.conn.cursor()
    def test(self):
        sql_query2 = 'show tables;'
        self.cursor.execute(sql_query2)
        print(self.cursor.fetchall())
    def get_all_name_from_table(self,tablename):
        sql="select COLUMN_NAME from information_schema.COLUMNS where table_name = '"+tablename+"' and table_schema = '"+self.database+"';"
        self.cursor.execute(sql)
        # 执行结果  [('id',), ('name',), ('url',), ('type',)]
        list = []
        for i in self.cursor.fetchall():
            list.append(i[0])
        # 返回结果 ['id', 'name', 'url', 'type']
        return list
    def get_all_success_case(self):
        databaseName = "success_case"
        sql = "select * from "+databaseName+";"
        self.cursor.execute(sql)
        data = self.cursor.fetchall()
        # 获取表含有的字段
        fileds = self.get_all_name_from_table(databaseName)
        result = []
        for i in range(len(data)):
            obj = {}
            for fi in range(len(fileds)):
                obj[fileds[fi]] = data[i][fi]
            result.append(obj)
        return result


    
        

